package com.dco.signature.controller;

import com.dco.signature.model.Signature;
import com.dco.signature.model.ErrorResponse;
import com.dco.signature.service.DcoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/dco")
public class DcoController {

    @Autowired
    private DcoService dcoService;

    // 签署DCO
    @PostMapping("/sign")
    public ResponseEntity<?> signDco(@RequestParam String repoUrl,
                                     @RequestParam String name,
                                     @RequestParam String email) {
        try {
            Signature signature = dcoService.signDco(repoUrl, name, email);
            return ResponseEntity.ok(signature);
        } catch (Exception e) {
            ErrorResponse error = new ErrorResponse(e.getMessage());
            return ResponseEntity.badRequest().body(error);
        }
    }

    // 查询是否已签署
    @GetMapping("/status")
    public ResponseEntity<?> checkStatus(@RequestParam String repoUrl,
                                         @RequestParam String email) {
        boolean hasSigned = dcoService.hasSigned(repoUrl, email);
        return ResponseEntity.ok(hasSigned);
    }

    // 获取所有签署者
    @GetMapping("/signatures")
    public ResponseEntity<List<Signature>> getSignatures(@RequestParam String repoUrl) {
        List<Signature> signatures = dcoService.getSignatures(repoUrl);
        return ResponseEntity.ok(signatures);
    }
}
